package com.mr.base.alpha.dao.impl;

import com.mr.base.alpha.dos.OrganizationDO;
import com.mr.base.alpha.dao.OrganizationDao;
import com.mr.base.alpha.mappers.expand.OrganizationExpandMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;

import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;

import org.apache.ibatis.executor.BatchResult;
import com.mr.base.core.mybatis.utils.BatchResultUtil;

/**
 * (TOrganization)Dao实现类
 *
 * @author ray
 * @since 2022-01-20 09:56:07
 */
@Repository
public class OrganizationDaoImpl implements OrganizationDao {

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    @Override
    public int batchUpdate(List<OrganizationDO> doList) {
        if (CollectionUtils.isEmpty(doList)) {
            return 0;
        }

        try (SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH)) {
            OrganizationExpandMapper mapper = session.getMapper(OrganizationExpandMapper.class);

            return Lists.partition(doList, 3000).stream().mapToInt(o -> {
                mapper.batchUpdate(o);

                final List<BatchResult> batchResults = session.flushStatements();

                session.commit();
                session.clearCache();

                return BatchResultUtil.changeCount(batchResults);
            }).sum();
        }
    }

    @Override
    public int batchInsert(List<OrganizationDO> doList) {
        if (CollectionUtils.isEmpty(doList)) {
            return 0;
        }

        try (SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH)) {
            OrganizationExpandMapper mapper = session.getMapper(OrganizationExpandMapper.class);

            return Lists.partition(doList, 3000).stream().mapToInt(o -> {
                mapper.batchInsert(o);

                final List<BatchResult> batchResults = session.flushStatements();

                session.commit();
                session.clearCache();

                return BatchResultUtil.changeCount(batchResults);
            }).sum();
        }
    }

    @Override
    public int batchDelete(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return 0;
        }

        try (SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH)) {
            OrganizationExpandMapper mapper = session.getMapper(OrganizationExpandMapper.class);

            return Lists.partition(ids, 3000).stream().mapToInt(o -> {
                mapper.batchDeleteByPrimaryKeys(o);

                final List<BatchResult> batchResults = session.flushStatements();

                session.commit();
                session.clearCache();

                return BatchResultUtil.changeCount(batchResults);
            }).sum();
        }
    }

}

